
package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.gc.GameTagInfo;
import cn.egame.interfaces.gc.GameTagLinkInfo;
import com.egame.manage.biz.server.ManageClient;

public class GameTagServiceDao extends BaseDao {

	public GameTagServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}
	
	public List<Integer> listGameIdByGameTagId(int gameTagId,String name)
			throws ExceptionCommonBase {
		long d = Utils.getCurrentTime();
		String sql = "select g_id from t_game where game_status = " + EnumType.GameStatus.business.value();
		if(!Utils.stringIsNullOrEmpty(name)){
			sql += " and game_name like " + SqlUtils.QuataLikeStr(name);
		}
		List<Integer> ids = this.getListInteger(sql);
		sql = "SELECT g_id FROM t_game_tag_link WHERE ENABLE=1 and tag_id="
				+ gameTagId;
		if(ids != null && ids.size()>0){
			sql += " and g_id in (" + Utils.toString(ids) + ")";
		}
		//sql	+=  " and start_time<" + d	+ " AND end_time>" + d + " ORDER BY game_sort_no DESC";	
		sql	+=  " ORDER BY game_sort_no DESC";	
		
		System.out.println(sql);		
		return this.getListInteger(sql);
	}
	
	public List<Integer> listGidByNotTagId(int gameTagId,int gId,String name) throws ExceptionCommonBase{
		List<Integer> tagIds = listGameIdByGameTagId(gameTagId,null);		
		String sql = "select g_id from t_game where " 
				 + " game_status = " + EnumType.GameStatus.business.value();
		if(tagIds != null && tagIds.size()>0){
			sql += " and g_id not in (" + Utils.toString(tagIds) + ")";
		}
		if(gId>0){
			sql += " and g_id = " + gId;
		}
		if(!Utils.stringIsNullOrEmpty(name)){
			sql += " and game_name like " + SqlUtils.QuataLikeStr(name);
		}
		System.out.println(sql);
		return this.getListInteger(sql);
	}
	
	public int upper(int appId, long loginUserId, int gId, int tagId) throws RemoteException {
		GameTagLinkInfo bean  = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);
		if(bean != null){
			GameTagLinkInfo upperBean = this.getUpperTagLinkInfo(appId, loginUserId, tagId, bean.getGameSortNo());
			if(upperBean != null){
				this.updateGameSortNo(tagId, upperBean.getgId(), bean.getGameSortNo());
				this.updateGameSortNo(tagId, bean.getgId(), upperBean.getGameSortNo());
				return 1;
			}			
		}		
		return -1;
	}
	

	public int lower(int appId, long loginUserId, int gId, int tagId) throws RemoteException {
		GameTagLinkInfo bean  = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);
		if(bean != null){
			GameTagLinkInfo lowerBean = this.getLowerTagLinkInfo(appId, loginUserId, tagId, bean.getGameSortNo());
			if(lowerBean != null){
				this.updateGameSortNo(tagId, lowerBean.getgId(), bean.getGameSortNo());
				this.updateGameSortNo(tagId, bean.getgId(), lowerBean.getGameSortNo());
				return 1;
			}
		}		
		return -1;
	}

	public int top(int appId, long loginUserId, int gId, int tagId) throws RemoteException {
		GameTagLinkInfo bean  = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);
		int maxSortNo = this.getMaxGameSortNo(tagId);
		if(bean != null && bean.getGameSortNo()<maxSortNo){
			String sql = "update t_game_tag_link set game_sort_no = game_sort_no-1 where tag_id = " + tagId + " and game_sort_no >" + bean.getGameSortNo();
			this.executeUpdate(sql);			
			sql = "update t_game_tag_link set game_sort_no = " + maxSortNo + " where tag_id = " + tagId + " and g_id = " + gId;
			return this.executeUpdate(sql);
		}
		return -1;
	}

	public int down(int appId, long loginUserId, int gId, int tagId) throws RemoteException {
		GameTagLinkInfo bean  = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);
		int minSortNo = this.getMinGameSortNo(tagId);
		if(bean != null && bean.getGameSortNo()>minSortNo){
			String sql = "update t_game_tag_link set game_sort_no = game_sort_no + 1 where tag_id = " + tagId + " and game_sort_no < " + bean.getGameSortNo();
			this.executeUpdate(sql);			
			sql = "update t_game_tag_link set game_sort_no = " + minSortNo + " where tag_id = " + tagId + " and g_id = " + gId;
			return this.executeUpdate(sql);			
		}
		return -1;
	}

	public int addTagLink(int appId, long loginUserId, int gId, int tagId,int tagSortNo ,int gameSortNo) throws RemoteException {			
		GameTagLinkInfo linkInfo = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);
		if(linkInfo != null){
			enableTagLink(gId,tagId);
		}else{
			GameTagInfo tagBean = ManageClient.getInstance().getGameTagInfoById(appId, loginUserId, tagId);	
			long time = new Date().getTime();
			if(tagBean != null){
				String sql = "select count(*) from t_game_tag_link where tag_id = " + tagId;
				int total = this.getInt(sql);
				if(gameSortNo>=0)total = gameSortNo;
				sql = "INSERT INTO t_game_tag_link ( g_id, tag_id, game_sort_no, tag_sort_no, ENABLE, hot_flag,start_time,end_time) VALUES (" +
						+ gId + ","
						+ tagId + ","
						+ total + ","
						+ tagSortNo + ","
						+ "1,0," 
						+ time 	+ "," 
						+ (time + 1000*3600*24*5*365L)  + ")";
				System.out.println(sql);
				return this.executeUpdate(sql);
			}
		}
		
		return -1;
	}
	
	public int deleteTagLink(int gId,int tagId)throws ExceptionCommonBase {
		String sql = "delete from t_game_tag_link where tag_id = " + tagId + " and g_id=" + gId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public int deleteTag(int tagId)throws ExceptionCommonBase {
		String sql = "update t_game_tag set enable = 0 where tag_id = " + tagId;
		return this.executeUpdate(sql);
	}
	
	public int disableTagLinkByGid(int gId)throws ExceptionCommonBase {
		String sql = "update t_game_tag_link set enable = 0 where g_id = " + gId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int enableTagLinkByGid(int gId)throws ExceptionCommonBase {
		String sql = "update t_game_tag_link set enable = 1 where g_id = " + gId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public int enableTagLink(int gId,int tagId)throws ExceptionCommonBase {
		String sql = "update t_game_tag_link set enable = 1 where g_id = " + gId + " and tag_id = " + tagId;
		return this.executeUpdate(sql);
	}

	public int addTag(GameTagInfo bean) throws ExceptionCommonBase {
		if(bean != null){
			String sql = "insert into t_game_tag(tag_name,tag_type,enable,remark)values(" 
				+ SqlUtils.QuataStr(bean.getTagName()) + ","
				+ bean.getTagType().value() + ","
				+ "1,"
				+ SqlUtils.QuataStr(bean.getRemark()) + ")";
			return this.executeUpdate(sql);
		}
		return -1;
	}
	
	public List<String> getNameList() throws ExceptionCommonBase {
		String sql = "select distinct tag_name from t_game_tag where tag_type = " + GameTagType.searchTag.value();
		return this.getListString(sql);
	}
	
	public GameTagLinkInfo getUpperTagLinkInfo(int appId,Long loginUserId,int tagId,int gameSortNo)throws RemoteException{		
		String sql = "SELECT g_id FROM t_game_tag_link WHERE tag_id = " + tagId + " AND game_sort_no > " + gameSortNo + " ORDER BY game_sort_no ASC LIMIT 1";	
		int gId = this.getInt(sql);
		return ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);		
	}

	public GameTagLinkInfo getLowerTagLinkInfo(int appId,Long loginUserId,int tagId,int gameSortNo)throws RemoteException{		
		String sql = "SELECT g_id FROM t_game_tag_link WHERE tag_id = " + tagId + " AND game_sort_no < " + gameSortNo + " ORDER BY game_sort_no DESC LIMIT 1";	
		int gId = this.getInt(sql);
		return ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, gId, tagId);		
	}
	
	public int updateGameSortNo(int tagId,int gId,int gameSortNo)throws ExceptionCommonBase{
		String sql = "update t_game_tag_link set game_sort_no = " + gameSortNo + " where tag_id = " + tagId + " and g_id = " + gId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int getMaxGameSortNo(int tagId)throws ExceptionCommonBase{
		String sql = "select max(game_sort_no) from t_game_tag_link where tag_id = " + tagId;
		return this.getInt(sql);
	}
	
	public int getMinGameSortNo(int tagId)throws ExceptionCommonBase{
		String sql = "select min(game_sort_no) from t_game_tag_link where tag_id = " + tagId;
		return this.getInt(sql);
	}

	public int setGameTagInfo(GameTagInfo gameTagInfo) throws ExceptionCommonBase {
		int identity = 0;
		String sql = "INSERT INTO t_game_tag ( tag_name, tag_type, ENABLE, count_click, count_game, remark ) VALUES ("
				+ SqlUtils.QuataStr(gameTagInfo.getTagName())+" , "
				+ gameTagInfo.getTagType().value()+" , "
				+ gameTagInfo.getEnable()+" , "
				+ gameTagInfo.getCountClick()+" , "
				+ gameTagInfo.getCountGame()+" , "
				+ SqlUtils.QuataStr(gameTagInfo.getRemark()) + ")";
		identity = this.getIdentityId(sql);
		return identity;
	}
}
